﻿using System;
using System.Collections.Generic;
using System.Text;

namespace lxwmvc
{
    using System;
    using System.Xml;
    using System.Web;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Data;
 
    /// <summary>
    /// 表单的帮助类
    /// </summary>
    public class formhelper
    {
        /// <summary>
        /// 设置字段日期默认值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string defaultdate(object key)
        {
            return "<input type=hidden name=\"" + key.tostr().ToLower() +"\" default=date />";
        }

        /// <summary>
        /// 生产排序方法
        /// </summary>
        /// <param name="n"></param>
        /// <param name="cn"></param>
        /// <param name="ot"></param>
        /// <returns></returns>
        public static string paixu(string n, string cn, string ot)
        {
            //##onclick="paixu('nc','$!cn','$!ot')" style="padding-right:10px;cursor:hand;background:url('/content/content/js/no.png') no-repeat center right"
            string _1 = string.IsNullOrEmpty(ot) ? "no": ot;
            if (n == cn)
            {
                if (string.IsNullOrEmpty(ot))
                {
                    _1 = "no";
                }
                else
                {
                    _1 = ot == "desc" ? "desc" : "asc";
                }
            }
            else
            {
                ot = "";
                _1 = "no";
            }
            return " onclick=\"paixu('"+n+"','"+cn+"','"+ot+"')\" style=\"padding-right:10px;cursor:hand;background:url('/content/image/"+_1+".png') no-repeat center right\" ";
        } 

        /// <summary>
        /// 加载日期控件
        /// </summary>
        /// <param name="key">name</param>
        /// <param name="fmt">日期格式化</param>
        /// <param name="html">自定义一些内容</param>
        /// <returns></returns>
        public static string inputdate(object key,string fmt="yyyy-MM-dd",string html="")
        {
            return "<input type=text name=\"" 
                + key.tostr().ToLower() 
                + "\" " 
                + html 
                + " onclick=\"WdatePicker({dateFmt:'" + fmt + "'})\"  />";
        }

//        /// <summary>
//        /// 多选html生成
//        /// </summary>
//        /// <param name="key"></param>
//        /// <param name="drc"></param>
//        /// <returns></returns>
//        public static string duoxuan(string key, DataRowCollection drc)
//        {
//            string html = @"
//            <script>
//            function [key]_ck(ths) {
//                var obj = document.getElementsByName(""[key]"")[0];
//                var v = obj.value;
//                var vs = v.split('|');
//                if (ths.style.backgroundColor == ""yellow"") {
//                    ths.style.backgroundColor = """";
//                    for(i=0;i<vs.length;i++)if(vs[i]==ths.value)vs.splice(i,1);
//                }
//                else {
//                    ths.style.backgroundColor = ""yellow"";
//                    vs[vs.length] = ths.value;
//                }
//                obj.value = vs.join('|');
//            }
//
//            addonload(function(){
//                var obj = document.getElementsByName(""[key]"")[0];
//                var objv = document.getElementsByName(""[key]_v"");
//                var v = obj.value;
//                var vs = v.split('|');
//                for(i=0;i<objv.length;i++)
//                    for(j=0;j<vs.length;j++)
//                        if(objv[i].value == vs[j]) objv[i].style.backgroundColor = ""yellow"";
//            });
//        </script>
//            <input type=hidden name=""[key]"" readonly />".Replace("[key]",key);  

//            string htmlbuttons = "";
//            foreach(DataRow dr in drc)
//                htmlbuttons += "<input type=button name=\"[key]_v\" value=\"".Replace("[key]", key) + dr[0].tostr() + "\" onclick=\"[key]_ck(this)\" />".Replace("[key]", key);

//            return html + htmlbuttons + "(支持多选)";
            
//        }

//        /// <summary>
//        /// 单选html生成
//        /// </summary>
//        /// <param name="key"></param>
//        /// <param name="drc"></param>
//        /// <returns></returns>
//        public static string danxuan(string key, DataRowCollection drc)
//        {
//            string html = @"
//            <script>
//            function [key]_ck(ths) {
//                var obj = document.getElementsByName(""[key]"")[0];
//                var objarr = document.getElementsByName(""[key]_v"");
//                var v = obj.value;
//                //var vs = v.split('|');
//                if (ths.style.backgroundColor == ""yellow"") {
//                    //代表已经选中
//                    return;
//                    //ths.style.backgroundColor = """";
//                    //for(i=0;i<vs.length;i++)if(vs[i]==ths.value)vs.splice(i,1);
//                }
//                else {
//                    var old = v;
//                    if(old != null && old != """")
//                    {
//                        for(m_i=0;m_i<objarr.length;m_i++)
//                        {
//                            if(objarr[m_i].value == old)
//                            {
//                                 objarr[m_i].style.backgroundColor = """";
//                                 break;
//                            }
//                        }
//                    }
//                    
//                    v = ths.value; 
//                    ths.style.backgroundColor = ""yellow"";
//                }
//                obj.value = v;
//            }
//
//            addonload(function(){
//                var obj = document.getElementsByName(""[key]"")[0];
//                var objv = document.getElementsByName(""[key]_v"");
//                var v = obj.value;
//                //var vs = v.split('|');
//                for(i=0;i<objv.length;i++)
//                    if(objv[i].value == vs) objv[i].style.backgroundColor = ""yellow"";
//            });
//        </script>
//            <input type=hidden name=""[key]"" readonly />".Replace("[key]", key);

//            string htmlbuttons = "";
//            foreach (DataRow dr in drc)
//                htmlbuttons += "<input type=button name=\"[key]_v\" value=\"".Replace("[key]", key) + dr[0].tostr() + "\" onclick=\"[key]_ck(this)\" />".Replace("[key]", key);

//            return html + htmlbuttons + "(只能单选)";

//        }

        /// <summary>
        /// 生成iframe并显示
        /// </summary>
        /// <param name="name">iframe的名称</param>
        /// <returns></returns>
        public static string iframe(string name)
        {
            return string.Format("<iframe name=\"{0}\" id=\"{0}\"  style=\"display:none\" ></iframe>", name);
        }

        /// <summary>
        /// 显示验证码
        /// </summary>
        /// <returns></returns>
        public static string code()
        {
            //显示验证码
            return string.Join("", new string[] { "<span onclick='this.innerHTML=\"/sys/code\"'><img src='/sys/code' /></span>", "" });
        }

        /// <summary>
        /// 生成隐藏域
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string hidden(object key,string value="")
        {
            return "<input type=hidden name=\"" + key + "\" value=\"" + value + "\" />";
        }

        /// <summary>
        /// 生成input
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string input(object key, object value, string html="")
        {
            return "<input type=text name=\"" + key + "\" value=\"" + value + "\" "+html+" />";
        }

        //public static string startform(string html = "")
        //{
        //    return "<form method='POST' enctype='multipart/form-data' target='iframe_data' " + html + " >";
        //}

//        /// <summary>
//        /// 生成上传图片代码
//        /// </summary>
//        /// <param name="name"></param>
//        /// <param name="value"></param>
//        /// <returns></returns>
//        public static string uploadimg(object name, object value,string html = "")
//        {
//            return @"
//<script>
//function upload_[name]() {
//   var result = window.showModalDialog('/sys/upload?t='+(new Date()).toString(), '', 'resizable:yes;scroll:yes;status:no;dialogWidth=400px;dialogHeight=40px;center=yes;help=no');
//   if (result != null && result.toString().length > 0) document.getElementsByName('[name]')[0].value = result;
//}
//function view_[name]()
//{
//   var url = document.getElementsByName('[name]')[0].value;
//   if (url.length > 0) window.open(url); 
//   else alert('没有图片!'); 
//}
//</script>
//<input name='[name]'  value='[value]' [html] />
//<input type=button value='上传图片' onclick='upload_[name]();'/>
//<input type=button value='查看图片' onclick='view_[name]();'/>
//    ".Replace("[name]", name.tostr()).Replace("[value]", value.tostr()).Replace("[html]",html);
//        }


//        public static string uploadimgex(object name, object value, object w,object h,object lj,object img,object dpi, string html = "")
//        {
//            return @"
//<script>
//function upload_[name]() {
//   var result = window.showModalDialog('/sys/uploadex?w=[w]&h=[h]&lj=[lj]&img=[img]&dpi=[dpi]&t='+(new Date()).toString(), '', 'resizable:yes;scroll:yes;status:no;dialogWidth=400px;dialogHeight=40px;center=yes;help=no');
//   if (result != null && result.toString().length > 0) document.getElementsByName('[name]')[0].value = result;
//}
//function view_[name]()
//{
//   var url = document.getElementsByName('[name]')[0].value;
//   if (url.length > 0) window.open(""[img]""+ url); 
//   else alert('没有图片!'); 
//}
//</script>
//<input name='[name]'  value='[value]' [html] />
//<input type=button value='上传图片' onclick='upload_[name]();'/>
//<input type=button value='查看图片' onclick='view_[name]();'/>（图片最低要求:[w] x [h] ）
//    ".Replace("[name]", name.tostr()).Replace("[value]", value.tostr()).Replace("[html]", html).Replace("[w]", w.tostr()).Replace("[h]", h.tostr()).Replace("[lj]", lj.tostr()).Replace("[img]", img.tostr()).Replace("[dpi]", dpi.tostr());
//        }

//        /// <summary>
//        /// 上传文件
//        /// </summary>
//        /// <param name="name"></param>
//        /// <param name="value"></param>
//        /// <returns></returns>
//        public static string uploadfile(object name, object value, object value2)
//        {
//            return @"
//<script>
//function upload_[name]() {
//   var result = window.showModalDialog('/sys/uploadfile?t='+(new Date()).toString(), '', 'resizable:yes;scroll:yes;status:no;dialogWidth=400px;dialogHeight=40px;center=yes;help=no');
//   if (result != null && result.length > 0)
//   {
//       document.getElementsByName('[name]mingcheng')[0].value = result[0];
//       document.getElementsByName('[name]')[0].value = result[1];
//   }
//}
//function view_[name]()
//{
//   var url = document.getElementsByName('[name]')[0].value;
//   var name = document.getElementsByName('[name]mingcheng')[0].value;
//   if (url.length > 0) window.open('/sys/download?filename='+escape(name)+'&filepath='+escape(url)); 
//   else alert('没有文件!'); 
//}
//</script>
//<input name='[name]mingcheng' type=hidden value='[value2]' />
//<input name='[name]' readonly value='[value]' />
//<input type=button value='上传文件' onclick='upload_[name]();'/>
//<input type=button value='查看文件' onclick='view_[name]();'/>
//    ".Replace("[name]", name.tostr()).Replace("[value]", value.tostr()).Replace("[value2]", value2.tostr());
//        }

        /// <summary>
        /// 其他隐藏默认值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string defaultvalue(object key, object value)
        {
            return "<input type=hidden name=\"_" + key.tostr().ToLower() + "\" default=\"" + value.tostr() + "\" />";
        }

        /// <summary>
        /// 需要搜索的表达式生成
        /// </summary>
        /// <param name="key">name</param>
        /// <param name="express">查询表达式</param>
        /// <param name="value">对应的值</param>
        /// <returns></returns>
        public static string inputsearch(object key, object express, object value)
        {
            return "<input name=\"" 
                + key.tostr().ToLower() 
                + "\" search=\"true\" express=\"" 
                + express.tostr().ToLower() 
                + "\" value=\"" + value + "\" />";
        }

        /// <summary>
        /// 生成textarea框
        /// </summary>
        /// <param name="key"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="html"></param>
        /// <param name="value">对应的值</param>
        /// <returns></returns>
        public static string textarea(string key,string width="400px",string height="40px",string html="",string value="")
        {
            return @"<textarea name=""" + key + @""" style=""width:" + width + ";height:" + height + @""" "+html+">"+value+"</textarea>";
        }

        /// <summary>
        /// 生成ckeditor
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string ckeditor(object id)
        {
            return @"
<textarea id=""[id]"" name=""[id]"" cols=""100"" rows=""8"" style=""width:700px;height:400px;visibility:hidden;""></textarea> 

<script>
			var editor_[id];
			KindEditor.ready(function(K) {               
				editor_[id] = K.create('textarea[name=""[id]""]', {
				uploadJson : '/kindeditor/handler/upload_json.ashx',
				fileManagerJson : '/kindeditor/handler/file_manager_json.ashx',
				allowFileManager : true
				});                
			});
        
            addonload(function()
            {
                try{                                       
                editor_[id].html(DATA.[id]);
                }catch(e){}
            });
		</script>

".Replace("[id]", id.tostr());
        }
    }
}
